package com.ll.beautymall.ui.home.homefragment.model;

import com.ll.beautymall.bean.Content;
import com.ll.beautymall.bean.Floor;
import com.ll.beautymall.bean.NavList;
import com.ll.beautymall.utils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/10/20.
 * 具体加载首部导航条的数据,首页和9块9 的导航条数据结构一致
 */
public class NavBiz {
    public void getNavData(final OnGetNavResultLisenter lisenter) {
        HttpUtils.getInstance().getNavData(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    String jsonStr = response.body();
                    NavList result = parseJson(jsonStr);
                    if (result != null && lisenter != null) {
                        lisenter.onSucced(result);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                lisenter.onFailed("网络连接较慢，请检查网络...");
            }
        });
    }

    private NavList parseJson(String jsonStr) {
        try {
            NavList navList = new NavList();
            JSONObject obj = new JSONObject(jsonStr);
            JSONObject data = obj.getJSONObject("data");
            String max_selected_count = data.getString("max_selected_count");
            navList.setMax_selected_count(max_selected_count);
            JSONArray floor_items = data.getJSONArray("floor_items");
            List<Floor> floors = new ArrayList<>();
            for (int i = 0; i < floor_items.length(); i++) {
                Floor floor = new Floor();
                JSONObject jsonObject = floor_items.getJSONObject(i);
                int id = jsonObject.getInt("id");
                String title = jsonObject.getString("title");
                floor.setId(id);
                floor.setTitle(title);
                JSONArray content_items = jsonObject.getJSONArray("content_items");
                List<Content> contents = new ArrayList<>();
                for (int n = 0; n < content_items.length(); n++) {
                    JSONObject object = content_items.getJSONObject(n);
                    String content_type = object.getString("content_type");
                    String url_type = object.getString("url_type");
                    String request_url = object.getString("request_url");
                    JSONArray request_parameters = object.getJSONArray("request_parameters");
                    HashMap<String, String> map = new HashMap<>();
                    for (int m = 0; m < request_parameters.length(); m++) {
                        JSONObject parameters = request_parameters.getJSONObject(m);
                        String key = parameters.getString("key");
                        String value = parameters.getString("value");
                        map.put(key, value);
                    }
                    Content content = new Content(content_type, url_type, request_url, map);
                    contents.add(content);
                }
                floor.setContent(contents);
                floors.add(floor);
            }
            navList.setFloor(floors);
            String search_text = data.getString("search_text");
            navList.setSearch_text(search_text);
            return navList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}